package com.thoughtworks.kontinuum;

import com.thoughtworks.kontinuum.exception.KonException;
import com.thoughtworks.kontinuum.grabandmove.GrabAndMove;
import com.thoughtworks.kontinuum.help.HelpPrinter;

public class Kon {
	private static final String OPTION_SERVER 	   = "-s";
	private static final String OPTION_GRABANDMOVE = "-u";
	private static final String OPTION_REPORT	   = "-r";	
	private static final String OPTION_HELP		   = "-?";	

	public static void main(String [] args){
		(new Kon()).argsRouter(args);
	}
	
	/**
	 * Routes the request to the appropriate method
	 * @param args
	 */
	public void argsRouter(String [] args){
		if(args!=null && args.length>0){
			if(args[0].equals(OPTION_SERVER) && args[1]!=null){
				serverOption(args[1]);
			} else if(args[0].equals(OPTION_GRABANDMOVE) && args[1]!=null && args[2]!=null){
				optionGrabMoveAndUpdateReport(args[1], args[2]);
				optionReport();
			} else if(args[0].equals(OPTION_REPORT)){
				optionReport();
			} else {
				optionHelp();
			} 
		} else {
			optionHelp();
		}
	}
	
	/**
	 * Initiates the report server on specified port.
	 * @param port
	 */
	private void serverOption(String port){
		
	}
	
	/**
	 * Moves the report from directory where it was saved and move all content to the destination
	 * @param originDir
	 * @param destinationDir
	 */
	private void optionGrabMoveAndUpdateReport(String originDir, String destinationDir){
		try {
			GrabAndMove gm = new GrabAndMove(originDir, destinationDir);
			gm.doMove();
		} catch (KonException e) {
			System.out.println("An error has occurred: "+e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("An error has occurred: "+e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * Creates the report
	 */
	private void optionReport(){
		
	}
	
	private void optionHelp(){
		HelpPrinter.printHelp();
	}
}

